<template>
	<view class="list">

		<uni-swipe-action>
			<view v-for="(item,index) in noticeListData" :key="index">
				<uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler($event,item)">
					<view class="box">
						<view class="box-left">
							<view class="box-left-img">
								<image :src="item.senderHead"></image>
							</view>
						</view>
						<view class="box-center">
							<view class="box-name">
								<text>{{item.senderName}}</text>
							</view>
							<view class="box-title">
								<text>{{item.message}}</text>
							</view>
						</view>
						<view class="box-right">
							<view class="box-btn-0" v-if="item.state == 0">
								<view class="btn-js" @click="addFriendFun(item,'1')">
									<text>接受</text>
								</view>
								<view class="btn-jj" @click="addFriendFun(item,'2')">
									<text>拒绝</text>
								</view>
							</view>
							<view class="box-btn-1" v-if="item.state == 1">
								<view class="btn-msg">
									<text>已同意</text>
								</view>
							</view>
							<view class="box-btn-2" v-if="item.state == 2">
								<view class="btn-msg">
									<text>已拒绝</text>
								</view>
							</view>
						</view>
					</view>
				</uni-swipe-action-item>
			</view>
		</uni-swipe-action>
	</view>
</template>

<script>

	import {
		addFriend,
		addGroup
	} from '@/api/http/msg/system_msg.js'

	import {
		msgHomeData,
		msgChatData
	} from '@/object/msg.js'

	import {
		mapActions
	} from 'vuex'

	import {
		getNowTime
	} from '@/utils/time/time.js'

	export default {
		props: ['noticeListData'],

		data() {
			return {
				userId: uni.getStorageSync('userId'),
				nickname: uni.getStorageSync('nickname'),
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],

			}
		},
		methods: {
			...mapActions('m_chat', ['setMsgList', 'addChatData']),
			swipeActionClickHandler(e, item) {
				//console.log(item.id)
				var sysIds = item.id + ''
				this.$emit('cleanSystemMsg', sysIds)
			},

			addFriendFun(item, state) {
				var nowTime = getNowTime()
				var that = this
				item.state = state
				if (item.sysType == 2) {
					//console.log(item)
					addFriend(item).then(res => {
						if (res.data.pubInfo.code !== 0) {
							return uni.$showMsg(res.data.pubInfo.message);
						}
						//向好友发送消息
						if (state == '1') {
							let msgData = {
								type: this.$chatType.CHAT_FRIEND,
								msg: {
									senderId: uni.getStorageSync('userId'),
									senderName: uni.getStorageSync('nickname'),
									senderHead: uni.getStorageSync('head'),
									receiverId: item.senderId,
									receiverName: item.senderName,
									receiverHead: item.senderHead,
									message: '同意了添加好友请求',
									types: 6,
									time: nowTime,
									tip: 1
								}
							}
							uni.myWebsocket.socketTask.send({
								data: JSON.stringify(msgData),
								async success() {

									//id, type, name, head, news, time, msgNum, msgType
									// var addMsgHomeData = msgHomeData(
									// 	item.senderId,
									// 	2,
									// 	item.senderName,
									// 	item.senderHead,
									// 	'同意了添加好友请求',
									// 	nowTime + '',
									// 	0,
									// 	6
									// )
									// that.setMsgList(addMsgHomeData)

									// //msgId, tip, news, sendId, sendHead, sendNickname, msgType, time
									// var addMsgChatData = msgChatData(
									// 	1,
									// 	1,
									// 	'同意了添加好友请求',
									// 	uni.getStorageSync('userId'),
									// 	uni.getStorageSync('head'),
									// 	uni.getStorageSync('nickname'),
									// 	6,
									// 	nowTime + '',
									// 	2,
									// 	item.senderId
									// )

									// that.addChatData(addMsgChatData)

									//跳转聊天界面
									// setTimeout(() => {
									// 	var req = {
									// 		id: item.senderId,
									// 		nickname: item.senderName,
									// 		head: item.senderHead,
									// 		type: 2 //用户单聊类型
									// 	}
									// 	uni.reLaunch({
									// 		url: '/pages/msg/chat/chat?msgObject=' +
									// 			JSON.stringify(req),
									// 	})
									// }, 500);


								},
							});
							//console.log(msgData)

						} else {
							//不用处理
						}

						that.refData()
					})
					return
				}
				if (item.sysType == 3) {
					addGroup(item).then(res => {
						if (res.data.pubInfo.code !== 0) {
							return uni.$showMsg(res.data.pubInfo.message);
						}
						var busInfo = res.data.busInfo
						//向群聊发送消息
						if (state == '1') {
							let msgData = {
								type: 18,
								msg: {
									groupId: item.groupId,
									senderId: item.senderId,
									receiverId: this.userId,
									message: this.nickname + '进入群聊',
									types: 6,
									time: nowTime,
									groupHead: item.groupHead,
									groupName: item.groupName,
									tip: 0
								}
							}
							console.log(msgData)
							//console.log(item)
							var that = this
							uni.myWebsocket.socketTask.send({
								data: JSON.stringify(msgData),
								async success() {
									//id, type, name, head, news, time, msgNum, msgType
									// var addMsgHomeData = msgHomeData(
									// 	item.groupId,
									// 	3,
									// 	item.groupName,
									// 	item.groupHead,
									// 	busInfo.message,
									// 	busInfo.time,
									// 	0,
									// 	6
									// )
									// that.setMsgList(addMsgHomeData)

									// //msgId, tip, news, sendId, sendHead, sendNickname, msgType, time
									// var addMsgChatData = msgChatData(
									// 	busInfo.id,
									// 	busInfo.tip,
									// 	busInfo.message,
									// 	that.userId,
									// 	uni.getStorageSync('head'),
									// 	uni.getStorageSync('nickname'),
									// 	6,
									// 	busInfo.time,
									// 	3,
									// 	item.groupId
									// )

									// that.addChatData(addMsgChatData)

									//跳转聊天界面
									// setTimeout(() => {
									// 	var req = {
									// 		id: item.groupId,
									// 		nickname: item.groupName,
									// 		head: item.groupHead,
									// 		type: 3 //群聊类型
									// 	}
									// 	uni.reLaunch({
									// 		url: '/pages/msg/chat-group/chat-group?msgObject=' +
									// 			JSON.stringify(req),
									// 	})
									// }, 500);
								},
							});
							//console.log(msgData)

						} else {
							//不用处理
						}

						that.refData()
					})
					return
				}

			},
			//刷新页面数据
			refData() {
				this.$emit('refData')
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list {
		width: 100%;
	}

	.box {
		display: flex;
		width: 90%;
		margin-left: 5%;
		// height: 120rpx;
		padding: 20rpx 0;

		.box-left {
			margin-top: 10rpx;
			width: 20%;

			image {
				width: 100rpx;
				height: 100rpx;
			}
		}

		.box-center {
			width: 50%;

			.box-name {
				margin: 6rpx 0;

				text {
					color: rgba(56, 56, 56, 1);
					font-size: 30rpx;
					font-weight: 400;
				}
			}

			.box-title {
				text {
					color: rgba(166, 166, 166, 1);
					font-size: 20rpx;
					font-weight: 400;
				}
			}
		}

		.box-right {
			width: 30%;
			// display: flex;

			.box-btn-0 {
				display: flex;
				margin-top: 35rpx;

				.btn-jj {
					width: 90rpx;
					height: 50rpx;
					line-height: 50rpx;
					background: rgba(229, 229, 229, 1);
					border-radius: 60rpx;
					color: rgba(56, 56, 56, 1);
					font-size: 20rpx;
					font-weight: 400;
					text-align: center;
					margin-left: 20rpx;
				}

				.btn-js {
					width: 90rpx;
					height: 50rpx;
					line-height: 50rpx;
					background: rgba(245, 144, 168, 1);
					border-radius: 60rpx;
					color: rgba(255, 255, 255, 1);
					font-size: 20rpx;
					font-weight: 400;
					text-align: center;
				}

			}

			.box-btn-1 {
				.btn-msg {
					line-height: 120rpx;
					text-align: center;

					text {
						color: rgba(19, 176, 82, 1);
						font-size: 40rpx;
						font-weight: 400;
					}
				}

			}

			.box-btn-2 {
				.btn-msg {
					line-height: 120rpx;
					text-align: center;

					text {
						color: rgba(204, 204, 204, 1);
						font-size: 40rpx;
						font-weight: 400;
					}
				}

			}
		}
	}
</style>